package com.ndp.fb.walle.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ndp.fb.exception.CheckedException;
import com.ndp.fb.rdb.model.productmanage.ProductManageFeedExt;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ndp.ec.core.ProductManageService;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.exception.BusinessException;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.rdb.model.productmanage.ProductManage;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.util.ResponseUtils;

/**
 * Created by sisi.chen on 2016/1/5.
 */
@RequestMapping("/productmanage")
@Controller
public class ProductManageController {
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private ProductManageService productManageService;

    /**
     * 添加产品
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ResponseEntity<String> searchProductConfiguration(
            @RequestParam(value = "param", required = true) String param,
            HttpServletRequest request) {
        logger.info("ProductManageController save() begin.");
        ErrorType errorType = null;
        Map<String, Long> data = new HashMap<>();
        try {
            ProductManage productManage = JSON
                    .parseObject(param, ProductManage.class);
            User user = (User) request.getSession().getAttribute("user");
            productManage.setOrgId(user.getOrgId());
            productManage.setUserId(user.getTicket());
            Long id = productManageService.save(productManage);
            data.put("id", id);
        } catch (BusinessException be){
            errorType = be.getErrorType();
            logger.error("", be);
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error("出现异常", e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType,
                data);
        logger.info("ProductManageController save() end.");
        return ResponseUtils
                .returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }

    @RequestMapping(value = "get")
    @ResponseBody
    public List<ProductManage> get(HttpServletRequest req)throws Exception{
        User user = (User) req.getSession().getAttribute("user");
        return productManageService.findByOrgId(user.getOrgId());
    }

    /**
     * 获取公司下的全量产品，与产品管理不关联
    * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findAllByOrgId")
    @ResponseBody
    public List<ProductManage> findAllByOrgId(HttpServletRequest req)throws Exception{
        User user = (User) req.getSession().getAttribute("user");
        return productManageService.findAllByOrgId(user.getOrgId());
    }


    /**
     * 获取公司下的全量产品，与产品管理不关联
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "findPmAndFeedAllByOrgId")
    @ResponseBody
    public List<ProductManageFeedExt> findPmAndFeedAllByOrgId(HttpServletRequest req) throws Exception {
        User user = (User) req.getSession().getAttribute("user");
        try {
            return productManageService.findPmAndFeedAllByOrgId(user.getOrgId());
        } catch (Exception e) {
            logger.error("error >>>", e);
           throw new CheckedException(ErrorType.INTERNAL_EXCEPTION);
        }
    }



}
